import { publicRegExp, passWordRegExp, diGazSign } from '@/utils/regExp'

const validateNickName = (rule, value, callback) => {
  if (!value && value !== 0) {
    callback(new Error('请输入设备名称!'))
  } else {
    if (publicRegExp.test(value)) {
      callback(new Error('只能输入中文、英文字母、数字和下划线'))
    } else if (value.trim().length > 32 || value.trim().length < 4) {
      callback(new Error('长度限制4~32'))
    } else {
      callback()
    }
  }
}

const validateDeviceName = (rule, value, callback) => {
  if (!value && value !== 0) {
    callback(new Error('请输入设备ID'))
  } else {
    if (!diGazSign.test(value)) {
      callback(new Error('英文字母、数字和特殊字符-_@.:，长度限制4~32'))
    } else {
      callback()
    }
  }
}

const validateLocation = (rule, value, callback) => {
  if (!value && value !== 0) {
    callback(new Error('请输入设备位置!'))
  } else {
    if (publicRegExp.test(value)) {
      callback(new Error('只能输入中文、英文字母、数字和下划线'))
    } else if (value.trim().length > 32 || value.trim().length < 4) {
      callback(new Error('长度限制4~32'))
    } else {
      callback()
    }
  }
}

export default {
  genRuleValidate() {
    return {
      companyId: [
        { required: true, message: '请选择所属企业！', trigger: 'change' }
      ],
      groupId: [
        { required: true, message: '请选择所属区域！', trigger: 'change' }
      ],
      productKey: [
        { required: true, message: '请选择所属产品', trigger: 'change' }
      ],
      parentId: [
        { required: true, message: '请选择所属网关', trigger: 'change' }
      ],
      nickName: [
        { required: true, validator: validateNickName, trigger: 'blur' }
      ],
      deviceName: [
        { required: true, validator: validateDeviceName, trigger: 'blur' }
      ],
      location: [
        { required: true, validator: validateLocation, trigger: 'blur' }
      ]
    }
  },
  genColumns(vm) {
    return [
      {
        title: 'Iot Id',
        align: 'center',
        key: 'iotId'
      },
      {
        title: '设备ID',
        align: 'center',
        key: 'deviceName'
      },
      {
        title: '设备名称',
        align: 'center',
        key: 'nickName'
      },
      {
        title: '所属产品',
        align: 'center',
        key: 'productName'
      },
      {
        title: '所属企业',
        align: 'center',
        key: 'companyName'
      },
      {
        title: '设备位置',
        align: 'center',
        key: 'location'
      },
      {
        title: '报警状态',
        align: 'center',
        key: 'alarmStatusText',
        render(h, params) {
          const {
            row: { alarmStatusText }
          } = params
          return h('span', alarmStatusText ? alarmStatusText : '-')
        }
      },
      {
        title: '在线状态',
        align: 'center',
        key: 'statusText',
        render(h, params) {
          const {
            row: { statusText }
          } = params
          return h('span', statusText ? statusText : '-')
        }
      },
      {
        title: '更新时间',
        align: 'center',
        key: 'updatedAt',
        render(h, params) {
          const {
            row: { updatedAt }
          } = params
          return h(
            'span',
            updatedAt ? vm.moment(updatedAt).format('YYYY-MM-DD HH:mm:ss') : '-'
          )
        }
      },
      {
        title: '操作',
        align: 'center',
        width: 220,
        render(h, params) {
          const { row } = params
          const { status } = row
          const btnArray =
            status === 'DISABLE'
              ? [
                  h(
                    'Poptip',
                    {
                      props: {
                        confirm: true,
                        title: '您确定要解禁该设备吗?',
                        transfer: true,
                        cancelText: '取消'
                      },
                      on: {
                        'on-ok': () => {
                          vm.handleEnable(row.id)
                        }
                      }
                    },
                    [
                      h(
                        'Button',
                        {
                          props: {
                            size: 'small',
                            type: 'success'
                          }
                        },
                        '解禁'
                      )
                    ]
                  )
                ]
              : [
                  h(
                    'Poptip',
                    {
                      props: {
                        confirm: true,
                        title: '您确定要禁用该设备吗?',
                        transfer: true,
                        cancelText: '取消'
                      },
                      on: {
                        'on-ok': () => {
                          vm.handleDisable(row.id)
                        }
                      }
                    },
                    [
                      h(
                        'Button',
                        {
                          props: {
                            size: 'small'
                          }
                        },
                        '禁用'
                      )
                    ]
                  )
                ]
          return h(
            'div',
            [
              h(
                'Button',
                {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      const { id, nodeType } = { ...row }
                      vm.$router.push(`/iot/device/${id}/${nodeType}`)
                    }
                  },
                  style: {
                    marginRight: '5px'
                  }
                },
                '查看'
              ),

              h(
                'Poptip',
                {
                  props: {
                    confirm: true,
                    title: '您确定要删除这条数据吗?',
                    transfer: true,
                    cancelText: '取消'
                  },
                  on: {
                    'on-ok': () => {
                      vm.handleDelete({ ...row })
                    }
                  }
                },
                [
                  h(
                    'Button',
                    {
                      props: {
                        type: 'error',
                        size: 'small'
                        // disabled:
                        //   true || params.row.name == '长沙平和堂五一广场店'
                      },
                      style: {
                        marginRight: '5px'
                      }
                    },
                    '删除'
                  )
                ]
              )
            ].concat(btnArray)
          )
        }
      }
    ]
  }
}
